package logic

import (
	"context"
	"fmt"
	"mini/minirpc/internal/svc"
	"mini/minirpc/minirpc"
	"mini/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *minirpc.ProductAddRequest) (*minirpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line
	project := mysql.Products{
		Image:  in.Image,
		Video:  in.Video,
		Name:   in.Name,
		Num:    in.Num,
		WhyBuy: in.WhyBuy,
		UserId: int(in.UserId),
	}

	err := mysql.DB.Debug().Model(&mysql.Products{}).Create(&project).Error
	if err != nil {
		return nil, fmt.Errorf("商品发布失败")
	}

	return &minirpc.ProductAddResponse{Id: int64(project.ID)}, nil
}
